package weaver.interfaces.zhlx.duss.action.faemodule.pdm;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.PdmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 01 Customer Spec Approval Planning Phase 流程调用临时料号接口
 * @ModifyLog:
 **/
public class WeaCSAPPlanningPhaseAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaCSAPPlanningPhaseAction>>>>>requestid=" + requestid);
        try {
            String message = "调用PDM系统接口返回:";
            JSONObject paramsJson = new JSONObject();
            JSONObject hearderJson = new JSONObject();
            hearderJson.put("TIMESTAMP", System.currentTimeMillis());
            hearderJson.put("INTERFACEID", "EC-PDM-01");
            hearderJson.put("FLOWNUMBER", requestid);
            paramsJson.put("header", hearderJson);
            RecordSet rs = new RecordSet();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String tempmm = Util.null2String(rs.getString("tempmm"));
            String generaldescription1 = Util.null2String(rs.getString("generaldescription1"));
            String customercode = Util.null2String(rs.getString("customercode"));
            String qybm = Util.null2String(rs.getString("qybm"));
            JSONObject dataJson = new JSONObject();
            dataJson.put("ITEMNUMBER", tempmm);
            dataJson.put("DESCRIPTION", generaldescription1);
            dataJson.put("CUSTOMERCODE", customercode);
            dataJson.put("REGION", qybm);
            paramsJson.put("data", dataJson);
            writeLog(">>>>>WeaCSAPPlanningPhaseAction>>>>>paramsJson=" + paramsJson);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaCSAPPlanningPhaseAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            JSONObject resultJson = PdmUtil.partNumber(paramsJson, dataMap);
            writeLog(">>>>>WeaCSAPPlanningPhaseAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                JSONObject retrundataJson = JSONObject.parseObject(resultJson.getString("data"));
                String status = Util.null2String(retrundataJson.getString("status"));
                if (!"200".equals(status)) {
                    message += Util.null2String(retrundataJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaCSAPPlanningPhaseAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}